package com.smart.reader.controller;

import com.smart.reader.common.Page;
import com.smart.reader.common.R;
import com.smart.reader.enums.SuccessResultCode;
import com.smart.reader.exception.CustomException;
import com.smart.reader.model.bo.ClassInfoAddBo;
import com.smart.reader.model.bo.ClassInfoUpdateBo;
import com.smart.reader.model.vo.ClassInfoVo;
import com.smart.reader.model.vo.StudentInfoVo;
import com.smart.reader.service.ClassInfoService;
import com.smart.reader.service.StudentInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

@RequestMapping("/api/v1/class/info")
@RestController
@Validated
@Api(tags = "班级管理", description = "班级管理相关接口")
public class ClassInfoController {
    @Resource
    private ClassInfoService classInfoService;
    
    @Resource
    private StudentInfoService studentInfoService;
    
    /**
     * 新增数据
     *
     * @param classInfo 实例对象
     * @return 实例对象
     */
    @ApiOperation("新增班级数据")
    @PostMapping("/add")
    public R add(@RequestBody @Valid ClassInfoAddBo classInfo) throws CustomException {
        classInfoService.add(classInfo);
        return R.success(SuccessResultCode.SUCCESS.getCode(), SuccessResultCode.SUCCESS.getMsg());
    }
    
    @ApiOperation("删除班级数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "班级ID", required = true)
    })
    @DeleteMapping("/delete/{id}")
    public R delete(@NotNull(message = "班级ID不能为空") @PathVariable("id") Long id) throws CustomException {
        classInfoService.delete(id);
        return R.success(SuccessResultCode.SUCCESS.getCode(), SuccessResultCode.SUCCESS.getMsg());
    }
    
    @ApiOperation("修改班级数据")
    @PostMapping("/edit")
    public R edit(@RequestBody @Valid ClassInfoUpdateBo classInfo) throws CustomException {
        classInfoService.edit(classInfo);
        return R.success(SuccessResultCode.SUCCESS.getCode(), SuccessResultCode.SUCCESS.getMsg());
    }
    
    @GetMapping("queryInfo")
    @ApiOperation("获取班级列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = true, dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true, dataType = "int", defaultValue = "10"),
    })
    public R<Page<ClassInfoVo>> queryInfo(int pageNum, int pageSize) {
        Page<ClassInfoVo> result = classInfoService.queryInfo(pageNum, pageSize);
        return R.success(SuccessResultCode.SUCCESS.getCode(), SuccessResultCode.SUCCESS.getMsg(), result);
    }
    
    @ApiOperation("根据班级ID查询班级详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "班级ID", required = true)
    })
    @GetMapping("/detail/{id}")
    public R<ClassInfoVo> detail(@NotNull(message = "班级ID不能为空") @PathVariable("id") Long id) {
        ClassInfoVo classInfoVo = classInfoService.detail(id);
        return R.success(SuccessResultCode.SUCCESS.getCode(), SuccessResultCode.SUCCESS.getMsg(), classInfoVo);
    }
    
    @ApiOperation("获取班级学生列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "班级ID", required = true)
    })
    @GetMapping("/students/{id}")
    public R<List<StudentInfoVo>> getStudents(@NotNull(message = "班级ID不能为空") @PathVariable("id") Long id) {
        List<StudentInfoVo> students = studentInfoService.queryByClassId(id);
        return R.success(SuccessResultCode.SUCCESS.getCode(), SuccessResultCode.SUCCESS.getMsg(), students);
    }
}
